import { dayjs } from 'element-plus'

export const formatTime = (time) => dayjs(time).format('YYYY年MM月DD日')

//将时间格式为 ‘2022-07-21T06:00:22.000+0000’转化为’MM-dd-yyyy’
export const formatDateDesc = (oldDate) => {
  // 方式1 转换为'yyyy-MM-dd HH:mm:ss'
  function add0(num) {
    return num < 10 ? '0' + num : num
  } // 个位数的值在前面补0
  const date = new Date(oldDate)
  const Y = date.getFullYear()
  const M = date.getMonth() + 1
  const D = date.getDate()

  const dateString = add0(M) + '-' + add0(D) + '-' + Y
  return dateString
}
//将时间格式为 ‘2022-07-21T06:00:22.000+0000’转化为’MM-dd-yyyy’
export const formatDate = (oldDate) => {
  // 方式1 转换为'yyyy-MM-dd HH:mm:ss'
  function add0(num) {
    return num < 10 ? '0' + num : num
  } // 个位数的值在前面补0
  const date = new Date(oldDate)
  const Y = date.getFullYear()
  const M = date.getMonth() + 1
  const D = date.getDate()

  const dateString = Y + '-' + add0(M) + '-' + add0(D)
  return dateString
}
//将时间格式为 ‘2022-07-21T06:00:22.000+0000’转化为’yyyy-MM-dd HH:mm:ss’或者’yyyy/MM/dd HH:mm:ss’
export const formatDateTime = (oldDate) => {
  // 方式1 转换为'yyyy-MM-dd HH:mm:ss'
  function add0(num) {
    return num < 10 ? '0' + num : num
  } // 个位数的值在前面补0
  const date = new Date(oldDate)
  const Y = date.getFullYear()
  const M = date.getMonth() + 1
  const D = date.getDate()
  const h = date.getHours()
  const m = date.getMinutes()
  const s = date.getSeconds()

  const dateString =
    Y +
    '-' +
    add0(M) +
    '-' +
    add0(D) +
    '  ' +
    add0(h) +
    ':' +
    add0(m) +
    ':' +
    add0(s)
  return dateString
}

//获取当天日期
export const getToDay = () => {
  const today = new Date()
  const year = today.getFullYear()
  const month = String(today.getMonth() + 1).padStart(2, '0') // 月份从 0 开始，因此要加 1
  const day = String(today.getDate()).padStart(2, '0')

  return `${year}-${month}-${day}`
}

//获取前一天日期
export const getPreDay = () => {
  // 获取当前日期
  const today = new Date()
  // 计算前一天的日期
  const yesterday = new Date(today)
  yesterday.setDate(today.getDate() - 1)
  // 格式化日期（可选）
  const year = yesterday.getFullYear()
  const month = String(yesterday.getMonth() + 1).padStart(2, '0') // 月份从0开始
  const day = String(yesterday.getDate()).padStart(2, '0')
  const formattedDate = `${year}-${month}-${day}`
  return formattedDate // 输出前一天的日期，格式为 YYYY-MM-DD
}
//获取一个月前日期
export const getPreMonthDate = () => {
  // 获取当前日期
  const today = new Date()
  // 计算一个月前的日期
  const oneMonthAgo = new Date(today)
  oneMonthAgo.setMonth(today.getMonth() - 1)
  // 格式化日期（可选）
  const year = oneMonthAgo.getFullYear()
  const month = String(oneMonthAgo.getMonth() + 1).padStart(2, '0') // 月份从0开始
  const day = String(oneMonthAgo.getDate()).padStart(2, '0')
  const formattedDate = `${year}-${month}-${day}`
  return formattedDate // 输出前一天的日期，格式为 YYYY-MM-DD
}

// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time
        .replace(new RegExp(/-/gm), '/')
        .replace('T', ' ')
        .replace(new RegExp(/\.[\d]{3}/gm), '')
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
